package org.jeecg.modules.demo.socket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.demo.socket.controller.PushController;
import org.jeecg.modules.demo.socket.entity.ZyAppDyna;
import org.jeecg.modules.demo.socket.entity.ZyAppInfo;
import org.jeecg.modules.demo.socket.mapper.ZyAppDynaMapper;
import org.jeecg.modules.demo.socket.mapper.ZyAppInfoMapper;
import org.jeecg.modules.demo.socket.service.IZyAppDynaService;
import org.jeecg.modules.demo.socket.service.PushService;
import org.jeecg.modules.demo.socket.until.FileSave;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: zy_app_dyna
 * @Author: jeecg-boot
 * @Date:   2023-07-11
 * @Version: V1.0
 */
@Service
@Slf4j
public class ZyAppDynaServiceImpl extends ServiceImpl<ZyAppDynaMapper, ZyAppDyna> implements IZyAppDynaService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ZyAppDynaMapper zyAppDynaMapper;

    @Autowired
     private ZyAppInfoMapper zyAppInfoMapper;

    @Autowired
    private PushService pushService;

    FileSave fileSave=new FileSave();

    @Override
    public void add(HttpServletRequest request, List<MultipartFile> photos, String content, List<String> user_ids) {
        SysUser sysUser= null;
        String StringPhoto= null;
        Timestamp timestamp= null;
        try {
            String userName= JwtUtil.getUserNameByToken(request);
            sysUser = sysUserMapper.getUserByName(userName);
            StringPhoto = "";
            for (MultipartFile photo : photos) {
                if (photo!=null){
                    String s=fileSave.fileSave(photo);
                    StringPhoto+="|||"+s;
                }
            }
            timestamp = new Timestamp(System.currentTimeMillis());
            ZyAppDyna zyAppDyna=new ZyAppDyna(sysUser.getId(),StringPhoto,content,null,timestamp);
            zyAppDynaMapper.insert(zyAppDyna);
            String id=zyAppDynaMapper.getIdByPhotos(StringPhoto);
            for (String userId : user_ids) {
                ZyAppInfo zyAppInfo=new ZyAppInfo(id,userId);
                zyAppInfoMapper.insert(zyAppInfo);
                pushService.pushMsgToOne(userId);
            }
        } catch (JeecgBootException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void feedBack(HttpServletRequest request, List<MultipartFile> fdPhotos, String fdBack, List<String> userIds, String dyan_id) {
        String StringPhoto= null;
        Timestamp timestamp= null;
        try {
            for (MultipartFile photo : fdPhotos) {
                if (photo!=null){
                    String s=fileSave.fileSave(photo);
                    StringPhoto+="|||"+s;
                }
            }
            timestamp = new Timestamp(System.currentTimeMillis());
            ZyAppDyna zyAppDyna=new ZyAppDyna(dyan_id,fdBack,StringPhoto,timestamp);
            zyAppDynaMapper.updateById(zyAppDyna);
            for (String userId : userIds) {
                ZyAppInfo zyAppInfo=new ZyAppInfo(dyan_id,userId);
                zyAppInfoMapper.insert(zyAppInfo);
            }
        } catch (JeecgBootException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Result<List<ZyAppDyna>> get(HttpServletRequest request) {
        List<ZyAppDyna> zyAppDynas=new ArrayList<>();
        SysUser sysUser= null;
        String userName= JwtUtil.getUserNameByToken(request);
        sysUser = sysUserMapper.getUserByName(userName);
        List<ZyAppInfo> zyAppInfos=zyAppInfoMapper.selectByUserId(sysUser.getId());
        for (ZyAppInfo zyAppInfo : zyAppInfos) {
            zyAppDynas.add(zyAppDynaMapper.selectById(zyAppInfo.getDynaId()));
        }
        return Result.OK(zyAppDynas);
    }
}
